<!DOCTYPE html>
<html lang="en-us">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">

  <title> Interfaces </title>

  <link rel="stylesheet" media="screen" href="/css/style.css" >
</head>
<body>

  <header class="site-header">
    <h3 class="site-title">
      <a href="https://mkaz.github.io/working-with-go/">
        Working with Go
      </a>
    </h3>
  </header>

  <main>

    <aside>
      <h2>Contents</h2>
      <ul>
        
          <li><a href="/hello.html">Hola Mundo</a></li>
        
          <li><a href="/strings.html">Strings</a></li>
        
          <li><a href="/slices.html">Slices</a></li>
        
          <li><a href="/functions.html">Functions</a></li>
        
          <li><a href="/control-structures.html">Control Structures</a></li>
        
          <li><a href="/for-loops.html">For Loops</a></li>
        
          <li><a href="/structs.html">Structs</a></li>
        
          <li><a href="/maps.html">Maps</a></li>
        
          <li><a href="/files.html">Files</a></li>
        
          <li><a href="/read-dir.html">Directories</a></li>
        
          <li><a href="/objects.html">Objects</a></li>
        
          <li><a href="/command-line.html">Command-line</a></li>
        
          <li><a href="/date-time.html">Date/Time</a></li>
        
          <li><a href="/sorting.html">Sorting</a></li>
        
          <li><a href="/json.html">JSON</a></li>
        
          <li><a href="/web-server.html">Web Server</a></li>
        
          <li><a href="/guess-game.html">Guessing Game</a></li>
        
          <li><a href="/rest-api.html">REST API</a></li>
        
          <li><a href="/regexps.html">Regexp</a></li>
        
          <li><a href="/memcache.html">Memcache</a></li>
        
          <li><a href="/mysql.html">MySQL</a></li>
        
          <li><a href="/interfaces.html">Interfaces</a></li>
        
          <li><a href="/testing.html">Testing</a></li>
        
      </ul>
    </aside>

    <article class="entry-content">
      <h1>Interfaces</h1>

<p>An interface in Go is a type that defines a set of methods. Interfaces are the core of Go&rsquo;s type system. A type is said to satisfy an interface if it implements all the methods defined in that interface.</p>

<h2>Empty Interface</h2>

<p>The simplest example is the empty <code>interface{}</code>. This empty interface defines no methods thus all types implement it. This is often used to define a function that accepts any type, for example <code>fmt.Print</code> accepts any type and defined using</p>

<pre><code class="language-go">func Print(a ...interface{}) (n int, err error) {
	return Fprint(os.Stdout, a...)
}
</code></pre>

<p>The <code>...</code> is the spread operator, it allows for any number of <code>interface{}</code> types as arguments to the function.</p>

<h2>Interface with Methods</h2>

<p>Here is an example interface defining a single method. This defines a <code>Vehicle</code> interface as one that has the method <code>Alert()</code></p>

<pre><code class="language-go">type Vehicle interface {
	Alert() string
}
</code></pre>

<p>Any type that implements the <code>Alert()</code> method is said to satisfy the <code>Vehicle</code> interface.</p>

<p>When defining a struct you do not explicitly specify the interface it implements. Go determines automatically if a given type satisfies an interface if it defines all the methods.</p>

<pre><code class="language-go">type Car struct { }

func (c Car) Alert() string {
	return &quot;Honk! Honk!&quot;
}
</code></pre>

<p>Defining another type:</p>

<pre><code class="language-go">type Bicycle struct { }

func (b Bicycle) Alert() string {
	return &quot;Ring! Ring!&quot;
}
</code></pre>

<p>With the above definitions, you can create an array of Vehicles like so:</p>

<pre><code class="language-go">package main

import &quot;fmt&quot;

type Vehicle interface {
	Alert() string
}

type Car struct { }

func (c Car) Alert() string {
	return &quot;Honk! Honk!&quot;
}

type Bicycle struct { }

func (b Bicycle) Alert() string {
	return &quot;Ring! Ring!&quot;
}

func main() {
	c := Car{}
	b := Bicycle{}

	vehicles := []Vehicle{c, b}
	for _, v := range vehicles {
		fmt.Println(v.Alert())
	}
}
</code></pre>

    </article>

  </main>

<footer class="site-footer" role="contentinfo">
    An <a href="https://mkaz.blog/">mkaz</a> joint
    <span class="sep"> | </span>
    <a href="https://github.com/mkaz/hasite">Powered by Hastie</a>
</footer>

<script src="/js/asciinema/asciinema-player.js"></script>
<script src="/js/prism.js"></script>

</body>
</html>